﻿using StoryTeller.Domain;
using StoryTeller.Infrastructure.DataAccess.DAL.Base;
using StoryTeller.Infrastructure.DataAccess.Parser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StoryTeller.Infrastructure.DataAccess.DAL
{
    public class GroupDAL : BaseDAL, IGroupDAL
    {
        public void Insert(Group entidade)
        {
            Entities.Group.Add(entidade.ParseDomainToEntity());

            Save();
        }

        public void Update(Group entidade)
        {
            EGroup grp = Entities.Group.First(c => c.Id == entidade.Id);

            grp.LastUpdate = DateTime.Now;
            grp.Title = entidade.Title;

            Save();
        }

        public void Delete(int id)
        {
            EGroup grp = Entities.Group.First(c => c.Id == id);

            if (grp != null)
            {
                Entities.Group.Remove(grp);

                Save();
            }
        }

        public ICollection<Group> Select()
        {
            ICollection<EGroup> coll = Entities.Group.ToList();

            return coll.Select(c => c.ParseEntityToDomain()).ToList();
        }

        public Group Select(int id)
        {
            return Entities.Group.First(c => c.Id == id).ParseEntityToDomain();
        }

        public ICollection<ContributorRequest> SelectContributorRequests(Group group)
        {
            ICollection<EGroupContributorRequest> coll = Entities.GroupContributorRequest.Where(c => c.GroupId == group.Id).ToList();

            return coll.Select(c => c.ParseEntityToDomain()).ToList();
        }

        public void AddContributorRequest(ContributorRequest contributorRequest)
        {
            EGroupContributorRequest req = contributorRequest.ParseDomainToEntity();

            Entities.GroupContributorRequest.Add(req);

            Save();
        }

        public void AcceptContributorRequest(ContributorRequest contributorRequest)
        {
            EGroupContributorRequest req = Entities.GroupContributorRequest.First(c => c.Id == contributorRequest.Id);

            req.Accepted = true;

            EGroup grp = Entities.Group.First(g => g.Id == contributorRequest.ContributeIn.Id);
            EUser usr = Entities.User.First(g => g.Id == contributorRequest.Contributor.Id);

            grp.Users.Add(usr);

            Save();
        }

        public Pagination<Group> Select(Pagination<Group> pagination)
        {
            pagination.TotalItems = Entities.Group.Count();
            var query = from grp in Entities.Group
                        where
                            (string.IsNullOrEmpty(pagination.Filter.Title) || grp.Title == pagination.Filter.Title)
                        select grp;

            pagination.List = query.Skip((pagination.PageNumber - 1) * pagination.PageSize).Take(pagination.PageSize).Select(c => c.ParseEntityToDomain()).ToList();

            return pagination;
        }
    }
}
